package com.ruangong.GitClient.Test.GitHubAPITest;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ruangong.GitClient.GitHubAPI.GitHubRepository;
import com.ruangong.GitClient.GitHubAPI.MoMingUtil;
import com.ruangong.GitClient.GitHubAPI.GitHubUser;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于测试GitHub API的搜索功能。
 * 可以通过关键词进行仓库搜索，获取仓库的基本信息。
 */
public class Test3 {
    public static void main(String[] args) throws Exception{
        GitHubUser gitHubUser = new GitHubUser("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");
        gitHubUser.showInfo();
        String jsonResponse = MoMingUtil.searchGitHubRepository("Android"); // 假设这里是你从GitHub API获取的JSON字符串

        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(jsonResponse, JsonObject.class);
        JsonArray items = jsonObject.getAsJsonArray("items");
        List<GitHubRepository> repositories=new ArrayList<>();
        int cnt = 0;
        for (JsonElement item : items) {
            JsonObject repo = item.getAsJsonObject();
            System.out.println("Name: " + repo.get("name").getAsString());
            System.out.println("Description: " + (repo.get("description").isJsonNull() ? "" : repo.get("description").getAsString())); // 检查null
            System.out.println("Stars: " + repo.get("stargazers_count").getAsInt());
            System.out.println("URL: " + repo.get("html_url").getAsString());
            System.out.println("----------" + (++cnt) + "----------");

            MoMingUtil.showJson(repo.toString());
            repositories.add(GitHubRepository.fromJson(repo.toString()));
        }
        GitHubRepository repo = repositories.get(2);
        System.out.println(repo.toString());
    }
}
